/**
 * 配置文件
 */

const {
  override,
  fixBabelImports,
  addLessLoader,
  addPostcssPlugins,
  addWebpackExternals,
  addWebpackAlias,
} = require("customize-cra");

// 压缩 js 文件
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const path = require("path");
const myPlugin = [
  new UglifyJsPlugin({
    uglifyOptions: {
      warnings: false,
      compress: {
        drop_debugger: true,
        drop_console: true,
      },
    },
  }),
];

module.exports = {

  // webpack配置
  webpack: override(
    //不做打包处理配置，如直接以cdn引入的
    addWebpackExternals({
      // echarts: "window.echarts",
    }),
  
    //路径别名
    addWebpackAlias({
      "@": path.resolve(__dirname, "src"),
    }),
  
    // 按需加载 antd-mobile
    fixBabelImports("import", {
      libraryName: "antd-mobile",
      style: true,
    }),
  
    // 引入less
    addLessLoader({
      javascriptEnabled: true,
      modifyVars: {
        "@brand-primary": "#1cae82",
        "@brand-primary-tap": "#1DA57A",
        "color-text-base": "#333",
        "@font-size-icontext": "14px",
        "@font-size-caption-sm": "14px",
        "@font-size-base": "14px",
        "@font-size-subhead": "14px",
        "@font-size-caption": "14px",
        "@font-size-heading": "14px",
      },
    }),
  
    // 配置 px 转化为 rem
    addPostcssPlugins([
      require("postcss-pxtorem")({
        rootValue: 16,
        propList: ["*"],
      }),
    ]),
  
    //暴露webpack的配置 config ,evn
    (config) => {
      console.log("生产环境/开发环境:", process.env.NODE_ENV);
      // 生产环境
      if (process.env.NODE_ENV === "production") {
        config.devtool = false; // 去掉打包生产 map 文件
        config.plugins = [...config.plugins, ...myPlugin]; // 配置压缩js文件的插件
      }
      // 开发环境
      if (process.env.NODE_ENV === "development") {
      }
      return config;
    }
  ),
}


